Segmenting and Clustering Neighborhoods in Fredericton, NB

Applied Data Science Capstone Week 5 Peer-Graded Project Report

By Prakash Koshti - 6th Feb, 2019

Introduction to the opportunity

Fredericton is the Capital City of the only Canadian fully-bilingual Province of New Brunswick and is beautifully located on the banks of the Saint John River. While one of the least populated provincial capital cities with a population base of less than 60 thousand residents, it offers a wide spectrum of venues and is a governement, university and cultural hub.

As the city grows and develops, it becomes increasingly important to examine and understand it quantitiatively. The City of Fredericton provides open data for everyone and encourages entrepreneurial use to develop services for the benefit of its ciitzens.

Developers, investors, policy makers and/or city planners have an interest in answering the following questions as the need for additional services and citizen protection:

  1. What neighbourhoods have the highest crime?
  2. Is population density correlated to crime level?
  3. Using Foursquare data, what venues are most common in different locations within the city?
  4. Does the Knowledge Park really need a coffee shop?
  5. Does the Open Data project have specific enough or thick enough data to empower decisions to be made or is it too aggregate to provide value in its current detail?

Let's find out.

In [2]:
from PIL import Image
import requests
url = 'http://www.tourismfredericton.ca/sites/default/files/field/image/fredericton.jpg'
im = Image.open(requests.get(url, stream=True).raw)
im
Out[2]:

Data

To understand and explore we will need the following City of Fredericton Open Data:

  1. Open Data Site: http://data-fredericton.opendata.arcgis.com/
  2. Fredericton Neighbourhoods: http://data-fredericton.opendata.arcgis.com/datasets/neighbourhoods--quartiers
  3. Fredericton Crime by Neighbourhood: http://data-fredericton.opendata.arcgis.com/datasets/crime-by-neighbourhood-2017--crime-par-quartier-2017
  4. Fredericton Census Tract Demographics: http://data-fredericton.opendata.arcgis.com/datasets/census-tract-demographics--donn%C3%A9es-d%C3%A9mographiques-du-secteur-de-recensement
  5. Fredericton locations of interest: https://github.com/JasonLUrquhart/Applied-Data-Science-Capstone/blob/master/Fredericton%20Locations.xlsx
  6. Foursquare Developers Access to venue data: https://foursquare.com/

Using this data will allow exploration and examination to answer the questions. The neighbourhood data will enable us to properly group crime by neighbourhood. The Census data will enable us to then compare the population density to examine if areas of highest crime are also most densely populated. Fredericton locations of interest will then allow us to cluster and quantitatively understand the venues most common to that location.

This is going to be interesting...

Methodology

All steps are referenced beleow in the Appendix: Analysis section.

The methodology will include:

  1. Loading each data set
  2. Examine the crime frequency by neighbourhood
  3. Study the crime types and then pivot analysis of crime type frequency by neighbourhood
  4. Understand correlation between crimes and population density
  5. Perform k-means statisical analysis on venues by locations of interest based on findings from crimes and neighbourhood
  6. Determine which venues are most common statistically in the region of greatest crime count then in all other locations of interest.
  7. Determine if an area, such as the Knowledge Park needs a coffee shop.

Loading the data

After loading the applicable libraries, the referenced geojson neighbourhood data was loaded from the City of Fredericton Open Data site. This dataset uses block polygon shape coordinates which are better for visualization and comparison. The City also uses Ward data but the Neighbourhood location data is more accurate and includes more details. The same type of dataset was then loaded for the population density from the Stats Canada Census tracts.

The third dataset, an excel file, "Crime by Neighbourhood 2017" downloaded from the City of Fredericton Open Data site is found under the Public Safety domain. This dataset was then uploaded for the analysis. It's interesting to note the details of this dataset are aggregated by neighbourhood. It is not an exhaustive set by not including all crimes (violent offenses) nor specific location data of the crime but is referenced by neighbourhood.

This means we can gain an understanding of the crime volume by type by area but not specific enough to understand the distribution properties. Valuable questions such as, "are these crimes occuring more often in a specific area and at a certain time by a specific demographic of people?" cannot be answered nor explored due to what is reasonably assumed to be personal and private information with associated legal risks.

There is value to the city to explore the detailed crime data using data science to predict frequency, location, timing and conditions to best allocated resources for the benefit of its citizens and it's police force. However, human behaviour is complex requiring thick profile data by individual and the conditions surrounding the event(s). To be sufficient for reliable future prediction it would need to demonstrate validity, currency, reliability and sufficiency.

Exploring the data

Exploring the count of crimes by neighbourhood gives us the first glimpse into the distribution.

One note is the possibility neighbourhoods names could change at different times. The crime dataset did not mention which specific neighbourhood naming dataset it was using but we assumed the neighbourhood data provided aligned with the neighbourhoods used in the crime data. It may be beneficial for the City to note and timestamp neighbourhood naming in the future or simply reference with neighbourhood naming file it used for the crime dataset.

An example of data errors: There was an error found in the naming of the neighbourhood "Platt". The neighbourhood data stated "Plat" while the crime data stated "Platt". Given the crime dataset was most simple to manipulate it was modified to "Plat". The true name of the neighbourhood is "Platt".

First Visualization of Crime

Once the data was prepared, a choropleth map was created to view the crime count by neighbourhood. As expected the region of greatest crime count was found in the downtown and Platt neighbourhoods.

Examining the crime types enables us to learn the most frequent occuring crimes which we then plot as a bar chart to see most frequenty type.

Theft from motor vehicles is most prevalent in the same area as the most frequent crimes. It's interesting to note this area is mostly residential and most do not have garages. It would be interesting to further examine if surveillance is a deterant for motor vehicle crimes in the downtown core compared to low surveillance in the Platt neighbourhood.

Examining 2nd most common crime given it is specific: theft from vehicles

After exploring the pivot table showing Crime_Type by Neighbourhood, we drill into a specific type of crime, theft from vehicles and plot the choropleth map to see which area has the greatest frequency.

Again, the Platt neighbourhood appears as the most frequent.

Is this due to population density?

Introducing the Census data to explore the correlation between crime frequency and population density.

Visualising the population density enables us to determine that the Platt neighbourhood has lower correlation to crime frequency than I would have expected.

It would be interesting to further study the Census data and if this captures the population that is renting or more temporary/transient poplution, given the City is a University hub.

Look at specific locations to understand the connection to venues using Foursquare data

Loading the "Fredericton Locations" data enables us to perform a statistical analysis on the most common venues by location.

We might wonder if the prevalence of bars and clubs in the downtown region has something to do with the higher crime rate in the near Platt region.

Plotting the latitude and longitude coordinates of the locations of interest onto the crime choropleth map enables us to now study the most common venues by using the Foursquare data.

Analysing each Location

Grouping rows by location and the mean of the frequency of occurance of each category we venue categories we study the top five most common venues.

Putting this data into a pandas dataframe we can then determine the most common venues by location and plot onto a map.

Results

The analysis enabled us to discover and describe visually and quantitatively:

  1. Neighbourhoods in Fredericton

  2. Crime freqency by neighbourhood

  3. Crime type frequency and statistics. The mean crime count in the City of Fredericton is 22.

  4. Crime type count by neighbourhood. Theft from motor vehicles is most prevalent in the same area as the most frequent crimes. It's interesting to note this area is mostly residential and most do not have garages. It would be interesting to further examine if surveillance is a deterant for motor vehicle crimes in the downtown core compared to low surveillance in the Platt neighbourhood.

4.1 Motor Vehicle crimes less than $5000 analysis by neighbourhood and resulting statistics. The most common crime is Other Theft less than 5k followed by Motor Vehicle Theft less than 5k. There is a mean of 6 motor vehicle thefts less than 5k by neighbourhood in the City.

4.2 That population density and resulting visual correlation is not strongly correlated to crime frequency. Causation for crime is not able to be determined given lack of open data specificity by individual and environment.

4.3 Using k-menas, we were able to determine the top 10 most common venues within a 1 km radius of the centroid of the highest crime neighbourhood. The most common venues in the highest crime neighbourhood are coffee shops followed by Pubs and Bars.

While, it is not valid, consistent, reliable or sufficient to assume a higher concentration of the combination of coffee shops, bars and clubs predicts the amount of crime occurance in the City of Fredericton, this may be a part of the model needed to be able to in the future.

  1. We were able to determine the top 10 most common venues by location of interest.

  2. Statisically, we determined there are no coffee shops within the Knowledge Park clusters.

Discussion and Recommendations

The City of Fredericton Open Data enables us to gain an understanding of the crime volume by type by area but not specific enough to understand the distribution properties. Valuable questions such as, "are these crimes occuring more often in a specific area and at a certain time by a specific demographic of people?" cannot be answered nor explored due to what is reasonably assumed to be personal and private information with associated legal risks.

There is value to the city to explore the detailed crime data using data science to predict frequency, location, timing and conditions to best allocated resources for the benefit of its citizens and it's police force. However, human behaviour is complex requiring thick profile data by individual and the conditions surrounding the event(s). To be sufficient for reliable future prediction it would need to demonstrate validity, currency, reliability and sufficiency.

A note of caution is the possibility neighbourhoods names could change. The crime dataset did not mention which specific neighbourhood naming dataset it was using but we assumed the neighbourhood data provided aligned with the neighbourhoods used in the crime data. It may be beneficial for the City to note and timestamp neighbourhood naming in the future or simply reference with neighbourhood naming file it used for the crime dataset.

Errors exist in the current open data. An error was found in the naming of the neighbourhood "Platt". The neighbourhood data stated "Plat" while the crime data stated "Platt". Given the crime dataset was most simple to manipulate it was modified to "Plat". The true name of the neighbourhood is "Platt".

Theft from motor vehicles is most prevalent in the same area as the most frequent crimes. It is interesting to note this area is mostly residential and most do not have garages. It would be interesting to further examine if surveillance is a deterant for motor vehicle crimes in the downtown core compared to low surveillance in the Platt neighbourhood.

It would be interesting to further study the Census data and if this captures the population that is renting or more temporary/transient poplution, given the City is a University hub.

Given the findings of the top 10 most frequent venues by locations of interest, the Knowledge Park does not have Coffee Shops in the top 10 most common venues as determined from the Foursquare dataset. Given this area has the greatest concentration of stores and shops as venues, it would be safe to assume a coffee shop would be beneficial to the business community and the citizens of Fredericton.

Conclusion

Using a combination of datasets from the City of Fredericton Open Data project and Foursquare venue data we were able to analyse, discover and describe neighbhourhoods, crime, population density and statistically describe quantitatively venues by locations of interest.

While overall, the City of Fredericton Open Data is interesting, it misses the details required for true valued quantitiatve analysis and predictive analytics which would be most valued by investors and developers to make appropriate investments and to minimize risk.

The Open Data project is a great start and empowers the need for a "Citizens Like Me" model to be developed where citizens of digital Fredericton are able to share their data as they wish for detailed analysis that enables the creation of valued services.

In [ ]:
 

APPENDIX: Analysis

Load Libraries

In [1]:
import numpy as np # library to handle data in a vectorized manner

import pandas as pd # library for data analysis
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

import json # library to handle JSON files

!conda install -c conda-forge geopy --yes # uncomment this line if you haven't completed the Foursquare API lab
from geopy.geocoders import Nominatim # convert an address into latitude and longitude values

import requests # library to handle requests
from pandas.io.json import json_normalize # tranform JSON file into a pandas dataframe

# Matplotlib and associated plotting modules
import matplotlib.cm as cm
import matplotlib.colors as colors

# import k-means from clustering stage
from sklearn.cluster import KMeans

# for webscraping import Beautiful Soup 
from bs4 import BeautifulSoup

import xml

!conda install -c conda-forge folium=0.5.0 --yes 
import folium # map rendering library

print('Libraries imported.')
Solving environment: done

## Package Plan ##

  environment location: /home/jupyterlab/conda

  added / updated specs: 
    - geopy


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    geographiclib-1.49         |             py_0          32 KB  conda-forge
    cryptography-2.4.2         |   py36h1ba5d50_0         618 KB
    openssl-1.1.1a             |    h14c3975_1000         4.0 MB  conda-forge
    libarchive-3.3.3           |       h5d8350f_5         1.5 MB
    grpcio-1.16.1              |   py36hf8bcb03_1         1.1 MB
    geopy-1.18.1               |             py_0          51 KB  conda-forge
    conda-4.6.2                |           py36_0         869 KB  conda-forge
    libssh2-1.8.0              |                1         239 KB  conda-forge
    python-3.6.8               |       h0371630_0        34.4 MB
    ------------------------------------------------------------
                                           Total:        42.7 MB

The following NEW packages will be INSTALLED:

    geographiclib: 1.49-py_0                conda-forge

The following packages will be UPDATED:

    conda:         4.5.12-py36_1000         conda-forge --> 4.6.2-py36_0            conda-forge
    cryptography:  2.3.1-py36hb7f436b_1000  conda-forge --> 2.4.2-py36h1ba5d50_0               
    curl:          7.63.0-h646f8bb_1000     conda-forge --> 7.63.0-hbc83047_1000               
    geopy:         1.11.0-py36_0            conda-forge --> 1.18.1-py_0             conda-forge
    grpcio:        1.16.0-py36h4f00d22_1000 conda-forge --> 1.16.1-py36hf8bcb03_1              
    libcurl:       7.63.0-h01ee5af_1000     conda-forge --> 7.63.0-h20c2e04_1000               
    openssl:       1.0.2p-h14c3975_1002     conda-forge --> 1.1.1a-h14c3975_1000    conda-forge
    pycurl:        7.43.0.2-py36hb7f436b_0              --> 7.43.0.2-py36h1ba5d50_0            
    python:        3.6.6-hd21baee_1003      conda-forge --> 3.6.8-h0371630_0                   
    qt:            5.9.6-h8703b6f_2                     --> 5.9.7-h5867ecd_1                   

The following packages will be DOWNGRADED:

    krb5:          1.16.2-hc83ff2d_1000     conda-forge --> 1.16.1-h173b8e3_7                  
    libarchive:    3.3.3-ha149a29_1000      conda-forge --> 3.3.3-h5d8350f_5                   
    libssh2:       1.8.0-h1ad7b7a_1003      conda-forge --> 1.8.0-1                 conda-forge


Downloading and Extracting Packages
geographiclib-1.49   | 32 KB     | ##################################### | 100% 
cryptography-2.4.2   | 618 KB    | ##################################### | 100% 
openssl-1.1.1a       | 4.0 MB    | ##################################### | 100% 
libarchive-3.3.3     | 1.5 MB    | ##################################### | 100% 
grpcio-1.16.1        | 1.1 MB    | ##################################### | 100% 
geopy-1.18.1         | 51 KB     | ##################################### | 100% 
conda-4.6.2          | 869 KB    | ##################################### | 100% 
libssh2-1.8.0        | 239 KB    | ##################################### | 100% 
python-3.6.8         | 34.4 MB   | ##################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Collecting package metadata: done
Solving environment: done

# All requested packages already installed.

Libraries imported.
In [2]:
r = requests.get('https://opendata.arcgis.com/datasets/823d86e17a6d47808c6e4f1c2dd97928_0.geojson')
fredericton_geo = r.json()
In [3]:
neighborhoods_data = fredericton_geo['features']
In [4]:
neighborhoods_data[0]
Out[4]:
{'type': 'Feature',
 'properties': {'FID': 1,
  'OBJECTID': 1,
  'Neighbourh': 'Fredericton South',
  'Shape_Leng': 40412.2767429,
  'Shape_Area': 32431889.0002},
 'geometry': {'type': 'Polygon',
  'coordinates': [[[-66.6193489311946, 45.8688925859664],
    [-66.5986068312843, 45.8934317575498],
    [-66.5998465063764, 45.8962889533894],
    [-66.6005561754508, 45.8987959122414],
    [-66.6007627879662, 45.9004150599189],
    [-66.6005112596866, 45.9020341603803],
    [-66.5993703992758, 45.9049409211054],
    [-66.5983912356161, 45.9066536507875],
    [-66.5950405196063, 45.9110977503182],
    [-66.5924713378938, 45.9137165396725],
    [-66.5975198697905, 45.9151915074375],
    [-66.6016161874861, 45.9165914405789],
    [-66.6063862416448, 45.9184662957134],
    [-66.6102310310608, 45.9201848572716],
    [-66.6193938469588, 45.9264149777787],
    [-66.6194297795702, 45.9243466803461],
    [-66.6206694546623, 45.9221345790227],
    [-66.6241459348118, 45.9181100781124],
    [-66.6249634017204, 45.9177976046497],
    [-66.6258796833102, 45.917910095299],
    [-66.6292124330143, 45.9200348758374],
    [-66.632733828928, 45.9225720071846],
    [-66.6356353872957, 45.924409167803],
    [-66.6362731911474, 45.9249840491044],
    [-66.6381955858555, 45.9258900999313],
    [-66.6400281490351, 45.9272147820915],
    [-66.6469721261813, 45.9309512150791],
    [-66.6492628301558, 45.9324257247173],
    [-66.6501521622871, 45.9331254782868],
    [-66.6504306400252, 45.9337564984884],
    [-66.6505653873178, 45.9347436246005],
    [-66.6503587748024, 45.9357182382069],
    [-66.6520745569951, 45.9352246860213],
    [-66.6532513500173, 45.9350872403269],
    [-66.6541855979128, 45.9351122304785],
    [-66.6557756159657, 45.9353808738969],
    [-66.6597461695215, 45.9365616400027],
    [-66.6692323789218, 45.9408659130747],
    [-66.6702205257343, 45.9411720097543],
    [-66.6705888350008, 45.9415718069541],
    [-66.6717027459531, 45.9418654061867],
    [-66.6805601346545, 45.9456570693391],
    [-66.6808206460869, 45.945613344883],
    [-66.690998558256, 45.9498794400526],
    [-66.6932353633134, 45.9503791076107],
    [-66.6956697977334, 45.9504478115476],
    [-66.6955530167465, 45.9498607024316],
    [-66.695014027576, 45.9498607024316],
    [-66.6956248819692, 45.948261735435],
    [-66.699766115429, 45.9452510552052],
    [-66.6993978061625, 45.9450511702315],
    [-66.6996762839006, 45.9448512845371],
    [-66.6992271262585, 45.9446139193389],
    [-66.7022364824603, 45.9407722096716],
    [-66.7041049782513, 45.9393666396225],
    [-66.7046080348104, 45.9387919073835],
    [-66.7061441539463, 45.9390980155132],
    [-66.7051919397451, 45.9388543785676],
    [-66.7056949963042, 45.937405028971],
    [-66.706611277894, 45.9362430230541],
    [-66.7074107784969, 45.9356745059121],
    [-66.7087133356588, 45.9350435075345],
    [-66.7110938711618, 45.9342063302882],
    [-66.7122526978783, 45.9309262230525],
    [-66.7096026677901, 45.9293891917718],
    [-66.6746402369322, 45.9061285859908],
    [-66.6193489311946, 45.8688925859664]],
   [[-66.6934150263703, 45.938648223393],
    [-66.7001973067654, 45.9422339647247],
    [-66.6939180829294, 45.9467626619838],
    [-66.6912141539242, 45.9449262417569],
    [-66.6899475293736, 45.9445014828376],
    [-66.6890312477838, 45.9444702504357],
    [-66.6889683657139, 45.9443827996167],
    [-66.6899565125264, 45.9418404190785],
    [-66.6934150263703, 45.938648223393]],
   [[-66.6550120479742, 45.9291455121693],
    [-66.6557756159657, 45.9292704762017],
    [-66.6599797314954, 45.9309387190672],
    [-66.6629172224744, 45.9322757763752],
    [-66.6631867170597, 45.932475707408],
    [-66.6631238349898, 45.9327880982037],
    [-66.6619290756619, 45.9341813397283],
    [-66.6616146653125, 45.9340751297235],
    [-66.6601863440107, 45.934818595486],
    [-66.6591442982811, 45.9350997354041],
    [-66.6586053091106, 45.9351059829416],
    [-66.6564673187345, 45.9348748235837],
    [-66.6542933957469, 45.9340501391045],
    [-66.6529908385849, 45.9333129107794],
    [-66.652308118969, 45.9324569639043],
    [-66.652191337982, 45.9319696305845],
    [-66.6522721863576, 45.9313573339335],
    [-66.6520476075366, 45.9305825815444],
    [-66.6521284559121, 45.9301264722544],
    [-66.6524428662616, 45.9296016295261],
    [-66.6531166027247, 45.9293392062996],
    [-66.6540508506202, 45.9291580085852],
    [-66.6550120479742, 45.9291455121693]],
   [[-66.6318085641854, 45.8878357293373],
    [-66.6328775593735, 45.8879357750148],
    [-66.6341801165354, 45.8882108996987],
    [-66.6351502970423, 45.8885422980769],
    [-66.6362462416889, 45.8890987927924],
    [-66.6370098096804, 45.8896365239624],
    [-66.6381596532441, 45.8909183040123],
    [-66.6385818614276, 45.8918186586532],
    [-66.6387435581788, 45.8925689430378],
    [-66.6385908445805, 45.8940757335582],
    [-66.6327517952337, 45.900733882662],
    [-66.62923039932, 45.9050971942525],
    [-66.6276673307256, 45.9064848805016],
    [-66.626454605092, 45.9071974626627],
    [-66.6253856099039, 45.9076662617274],
    [-66.6230230407067, 45.9082913209882],
    [-66.6205077579111, 45.9084913384651],
    [-66.6180014582685, 45.9082413165064],
    [-66.6181092561025, 45.9082100636823],
    [-66.6170312777616, 45.9076037554142],
    [-66.6161239793246, 45.9068661756028],
    [-66.6150909167479, 45.9054972515047],
    [-66.6147944727041, 45.9047533927481],
    [-66.6146417591058, 45.9037907372083],
    [-66.6146956580229, 45.9030155998367],
    [-66.614974135761, 45.9020654166814],
    [-66.617345688111, 45.8989772091164],
    [-66.6203819937714, 45.8954199312614],
    [-66.6263468072579, 45.8892363524244],
    [-66.6281254715205, 45.8883672199348],
    [-66.6291315846387, 45.8880795903605],
    [-66.6304521081064, 45.8878732464875],
    [-66.6318085641854, 45.8878357293373]]]}}
In [5]:
g = requests.get('https://opendata.arcgis.com/datasets/6179d35eacb144a5b5fdcc869f86dfb5_0.geojson')
demog_geo = g.json()

demog_data = demog_geo['features']
demog_data[0]
Out[5]:
{'type': 'Feature',
 'properties': {'FID': 1,
  'OBJECTID': 501,
  'DBUID': '1310024304',
  'DAUID': '13100243',
  'CDUID': '1310',
  'CTUID': '3200002.00',
  'CTNAME': '0002.00',
  'DBuid_1': '1310024304',
  'DBpop2011': 60,
  'DBtdwell20': 25,
  'DBurdwell2': 22,
  'Shape_Leng': 0.00746165241824,
  'Shape_Area': 2.81310751889e-06,
  'CTIDLINK': 3200002,
  'Shape__Area': 2.81310897700361e-06,
  'Shape__Length': 0.00746165464503067},
 'geometry': {'type': 'Polygon',
  'coordinates': [[[-66.634784212921, 45.9519239912381],
    [-66.6351046935752, 45.9507605156138],
    [-66.6378263667982, 45.9510868696778],
    [-66.636944377136, 45.9521037018384],
    [-66.634784212921, 45.9519239912381]]]}}
In [6]:
crime_df = pd.read_csv("Crime_by_neighbourhood_2017.csv")
crime_df.head()
Out[6]:
Neighbourhood From_Date To_Date Crime_Code Crime_Type Ward City FID
0 Fredericton South 2017-01-05T00:00:00.000Z 2017-01-26T00:00:00.000Z 2120 B&E NON-RESIDNCE 7 Fredericton 1
1 Fredericton South 2017-03-04T00:00:00.000Z 2017-03-06T00:00:00.000Z 2120 B&E NON-RESIDNCE 7 Fredericton 2
2 Fredericton South 2017-05-07T00:00:00.000Z NaN 2120 B&E NON-RESIDNCE 12 Fredericton 3
3 Fredericton South 2017-06-20T00:00:00.000Z 2017-06-21T00:00:00.000Z 2120 B&E NON-RESIDNCE 12 Fredericton 4
4 Fredericton South 2017-07-09T00:00:00.000Z 2017-07-10T00:00:00.000Z 2120 B&E NON-RESIDNCE 7 Fredericton 5
In [7]:
crime_df.drop(['From_Date', 'To_Date'], axis=1,inplace=True)

What is the crime count by neighbourhood?

In [8]:
crime_data = crime_df.groupby(['Neighbourhood']).size().to_frame(name='Count').reset_index()
crime_data
Out[8]:
Neighbourhood Count
0 Barkers Point 47
1 Brookside 54
2 Brookside Estates 9
3 Brookside Mini Home Park 5
4 College Hill 41
5 Colonial heights 9
6 Cotton Mill Creek 4
7 Diamond Street 1
8 Doak Road 1
9 Douglas 3
10 Downtown 127
11 Dun's Crossing 18
12 Forest Hill 12
13 Fredericton South 85
14 Fulton Heights 36
15 Garden Creek 13
16 Garden Place 4
17 Gilridge Estates 3
18 Golf Club 7
19 Grasse Circle 1
20 Greenwood Minihome Park 2
21 Hanwell North 8
22 Heron Springs 3
23 Highpoint Ridge 5
24 Kelly's Court Minihome Park 1
25 Knob Hill 4
26 Knowledge Park 1
27 Lian / Valcore 7
28 Lincoln 13
29 Lincoln Heights 14
30 Main Street 78
31 Marysville 39
32 McKnight 4
33 McLeod Hill 3
34 Monteith / Talisman 12
35 Montogomery / Prospect East 16
36 Nashwaaksis 25
37 Nethervue Minihome Park 1
38 North Devon 113
39 Northbrook Heights 10
40 Platt 198
41 Poet's Hill 4
42 Prospect 81
43 Rail Side 3
44 Regiment Creek 1
45 Royal Road 7
46 Saint Mary's First Nation 25
47 Saint Thomas University 1
48 Sandyville 9
49 Serenity Lane 2
50 Shadowood Estates 5
51 Silverwood 12
52 Skyline Acrea 27
53 South Devon 68
54 Southwood Park 16
55 Springhill 1
56 Sunshine Gardens 10
57 The Hill 44
58 The Hugh John Flemming Forestry Center 3
59 University Of New Brunswick 15
60 Waterloo Row 9
61 Wesbett / Case 1
62 West Hills 5
63 Williams / Hawkins Area 17
64 Woodstock Road 41
65 Youngs Crossing 16
In [9]:
crime_data.describe()
Out[9]:
Count
count 66.000000
mean 22.121212
std 34.879359
min 1.000000
25% 3.000000
50% 9.000000
75% 23.250000
max 198.000000
In [10]:
crime_data.rename(index=str, columns={'Neighbourhood':'Neighbourh','Count':'Crime_Count'}, inplace=True)
crime_data
Out[10]:
Neighbourh Crime_Count
0 Barkers Point 47
1 Brookside 54
2 Brookside Estates 9
3 Brookside Mini Home Park 5
4 College Hill 41
5 Colonial heights 9
6 Cotton Mill Creek 4
7 Diamond Street 1
8 Doak Road 1
9 Douglas 3
10 Downtown 127
11 Dun's Crossing 18
12 Forest Hill 12
13 Fredericton South 85
14 Fulton Heights 36
15 Garden Creek 13
16 Garden Place 4
17 Gilridge Estates 3
18 Golf Club 7
19 Grasse Circle 1
20 Greenwood Minihome Park 2
21 Hanwell North 8
22 Heron Springs 3
23 Highpoint Ridge 5
24 Kelly's Court Minihome Park 1
25 Knob Hill 4
26 Knowledge Park 1
27 Lian / Valcore 7
28 Lincoln 13
29 Lincoln Heights 14
30 Main Street 78
31 Marysville 39
32 McKnight 4
33 McLeod Hill 3
34 Monteith / Talisman 12
35 Montogomery / Prospect East 16
36 Nashwaaksis 25
37 Nethervue Minihome Park 1
38 North Devon 113
39 Northbrook Heights 10
40 Platt 198
41 Poet's Hill 4
42 Prospect 81
43 Rail Side 3
44 Regiment Creek 1
45 Royal Road 7
46 Saint Mary's First Nation 25
47 Saint Thomas University 1
48 Sandyville 9
49 Serenity Lane 2
50 Shadowood Estates 5
51 Silverwood 12
52 Skyline Acrea 27
53 South Devon 68
54 Southwood Park 16
55 Springhill 1
56 Sunshine Gardens 10
57 The Hill 44
58 The Hugh John Flemming Forestry Center 3
59 University Of New Brunswick 15
60 Waterloo Row 9
61 Wesbett / Case 1
62 West Hills 5
63 Williams / Hawkins Area 17
64 Woodstock Road 41
65 Youngs Crossing 16
In [11]:
crime_data.rename({'Platt': 'Plat'},inplace=True)
crime_data.rename(index=str, columns={'Neighbourhood':'Neighbourh','Count':'Crime_Count'}, inplace=True)
crime_data
Out[11]:
Neighbourh Crime_Count
0 Barkers Point 47
1 Brookside 54
2 Brookside Estates 9
3 Brookside Mini Home Park 5
4 College Hill 41
5 Colonial heights 9
6 Cotton Mill Creek 4
7 Diamond Street 1
8 Doak Road 1
9 Douglas 3
10 Downtown 127
11 Dun's Crossing 18
12 Forest Hill 12
13 Fredericton South 85
14 Fulton Heights 36
15 Garden Creek 13
16 Garden Place 4
17 Gilridge Estates 3
18 Golf Club 7
19 Grasse Circle 1
20 Greenwood Minihome Park 2
21 Hanwell North 8
22 Heron Springs 3
23 Highpoint Ridge 5
24 Kelly's Court Minihome Park 1
25 Knob Hill 4
26 Knowledge Park 1
27 Lian / Valcore 7
28 Lincoln 13
29 Lincoln Heights 14
30 Main Street 78
31 Marysville 39
32 McKnight 4
33 McLeod Hill 3
34 Monteith / Talisman 12
35 Montogomery / Prospect East 16
36 Nashwaaksis 25
37 Nethervue Minihome Park 1
38 North Devon 113
39 Northbrook Heights 10
40 Platt 198
41 Poet's Hill 4
42 Prospect 81
43 Rail Side 3
44 Regiment Creek 1
45 Royal Road 7
46 Saint Mary's First Nation 25
47 Saint Thomas University 1
48 Sandyville 9
49 Serenity Lane 2
50 Shadowood Estates 5
51 Silverwood 12
52 Skyline Acrea 27
53 South Devon 68
54 Southwood Park 16
55 Springhill 1
56 Sunshine Gardens 10
57 The Hill 44
58 The Hugh John Flemming Forestry Center 3
59 University Of New Brunswick 15
60 Waterloo Row 9
61 Wesbett / Case 1
62 West Hills 5
63 Williams / Hawkins Area 17
64 Woodstock Road 41
65 Youngs Crossing 16
In [12]:
address = 'Fredericton, Canada'

geolocator = Nominatim()
location = geolocator.geocode(address)
latitude = location.latitude
longitude = location.longitude
print('The geograpical coordinate of Fredericton, New Brunswick is {}, {}.'.format(latitude, longitude))
/home/jupyterlab/conda/lib/python3.6/site-packages/ipykernel_launcher.py:3: DeprecationWarning: Using Nominatim with the default "geopy/1.18.1" `user_agent` is strongly discouraged, as it violates Nominatim's ToS https://operations.osmfoundation.org/policies/nominatim/ and may possibly cause 403 and 429 HTTP errors. Please specify a custom `user_agent` with `Nominatim(user_agent="my-application")` or by overriding the default `user_agent`: `geopy.geocoders.options.default_user_agent = "my-application"`. In geopy 2.0 this will become an exception.
  This is separate from the ipykernel package so we can avoid doing imports until
The geograpical coordinate of Fredericton, New Brunswick is 45.966425, -66.645813.
In [13]:
world_geo = r'world_countries.json' # geojson file

fredericton_1_map = folium.Map(location=[45.97, -66.65], width=1000, height=750,zoom_start=12)

fredericton_1_map
Out[13]:
In [14]:
fredericton_geo = r.json()

threshold_scale = np.linspace(crime_data['Crime_Count'].min(),crime_data['Crime_Count'].max(), 6,dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale[-1] = threshold_scale[-1]+1

fredericton_1_map.choropleth(geo_data=fredericton_geo, data=crime_data,columns=['Neighbourh', 'Crime_Count'],
    key_on='feature.properties.Neighbourh', threshold_scale=threshold_scale,fill_color='YlOrRd', fill_opacity=0.7, 
    line_opacity=0.1, legend_name='Fredericton Neighbourhoods')

fredericton_1_map
Out[14]:

Examine Crime Types

In [15]:
crimetype_data = crime_df.groupby(['Crime_Type']).size().to_frame(name='Count').reset_index()
crimetype_data
Out[15]:
Crime_Type Count
0 4
1 ARSON 5
2 ARSON BY NEG 1
3 ARSON-DAM.PROP. 4
4 B&E NON-RESIDNCE 51
5 B&E OTHER 58
6 B&E RESIDENCE 151
7 B&E STEAL FIREAR 3
8 MISCHIEF OBS USE 1
9 MISCHIEF TO PROP 246
10 MISCHIEF-DATA 2
11 MOTOR VEH THEFT 40
12 THEFT BIKE<$5000 63
13 THEFT FROM MV < $5000 356
14 THEFT FROM MV > $5000 5
15 THEFT OTH <$5000 458
16 THEFT OTH >$5000 9
17 THEFT OVER $5000 1
18 THEFT,BIKE>$5000 2
In [16]:
crimetype_data.describe()
Out[16]:
Count
count 19.000000
mean 76.842105
std 133.196706
min 1.000000
25% 2.500000
50% 5.000000
75% 60.500000
max 458.000000
In [17]:
crimepivot = crime_df.pivot_table(index='Neighbourhood', columns='Crime_Type', aggfunc=pd.Series.count, fill_value=0)
crimepivot
Out[17]:
City Crime_Code FID Ward
Crime_Type ARSON ARSON BY NEG ARSON-DAM.PROP. B&E NON-RESIDNCE B&E OTHER B&E RESIDENCE B&E STEAL FIREAR MISCHIEF OBS USE MISCHIEF TO PROP MISCHIEF-DATA MOTOR VEH THEFT THEFT BIKE<$5000 THEFT FROM MV < $5000 THEFT FROM MV > $5000 THEFT OTH <$5000 THEFT OTH >$5000 THEFT OVER $5000 THEFT,BIKE>$5000 ARSON ARSON BY NEG ARSON-DAM.PROP. B&E NON-RESIDNCE B&E OTHER B&E RESIDENCE B&E STEAL FIREAR MISCHIEF OBS USE MISCHIEF TO PROP MISCHIEF-DATA MOTOR VEH THEFT THEFT BIKE<$5000 THEFT FROM MV < $5000 THEFT FROM MV > $5000 THEFT OTH <$5000 THEFT OTH >$5000 THEFT OVER $5000 THEFT,BIKE>$5000 ARSON ARSON BY NEG ARSON-DAM.PROP. B&E NON-RESIDNCE B&E OTHER B&E RESIDENCE B&E STEAL FIREAR MISCHIEF OBS USE MISCHIEF TO PROP MISCHIEF-DATA MOTOR VEH THEFT THEFT BIKE<$5000 THEFT FROM MV < $5000 THEFT FROM MV > $5000 THEFT OTH <$5000 THEFT OTH >$5000 THEFT OVER $5000 THEFT,BIKE>$5000 ARSON ARSON BY NEG ARSON-DAM.PROP. B&E NON-RESIDNCE B&E OTHER B&E RESIDENCE B&E STEAL FIREAR MISCHIEF OBS USE MISCHIEF TO PROP MISCHIEF-DATA MOTOR VEH THEFT THEFT BIKE<$5000 THEFT FROM MV < $5000 THEFT FROM MV > $5000 THEFT OTH <$5000 THEFT OTH >$5000 THEFT OVER $5000 THEFT,BIKE>$5000
Neighbourhood
Barkers Point 0 0 0 0 2 7 7 1 0 7 0 2 2 8 0 11 0 0 0 0 0 0 0 2 7 7 1 0 7 0 2 2 8 0 11 0 0 0 0 0 0 0 2 7 7 1 0 7 0 2 2 8 0 11 0 0 0 0 0 0 0 2 7 7 1 0 7 0 2 2 8 0 11 0 0 0
Brookside 0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 50 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 50 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2 0 0 0 50 0 0 0 0 0 0 0 2 0 0 0 0 0 0 2 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0 2 0 0 0
In [19]:
crimetype_data.plot(x='Crime_Type', y='Count', kind='barh')
Out[19]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f33ee0caa20>

Let's examine theft from vehicles

In [20]:
mvcrime_df = crime_df.loc[crime_df['Crime_Type'] == 'THEFT FROM MV < $5000']
mvcrime_df
Out[20]:
Neighbourhood Crime_Code Crime_Type Ward City FID
18 Fredericton South 2142 THEFT FROM MV < $5000 7 Fredericton 19
19 Fredericton South 2142 THEFT FROM MV < $5000 7 Fredericton 20
20 Fredericton South 2142 THEFT FROM MV < $5000 7 Fredericton 21
21 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 22
22 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 23
23 Fredericton South 2142 THEFT FROM MV < $5000 7 Fredericton 24
24 Fredericton South 2142 THEFT FROM MV < $5000 7 Fredericton 25
25 Fredericton South 2142 THEFT FROM MV < $5000 7 Fredericton 26
26 Fredericton South 2142 THEFT FROM MV < $5000 11 Fredericton 27
27 Fredericton South 2142 THEFT FROM MV < $5000 11 Fredericton 28
28 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 29
29 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 30
30 Fredericton South 2142 THEFT FROM MV < $5000 7 Fredericton 31
51 Barkers Point 2142 THEFT FROM MV < $5000 6 Fredericton 52
52 Barkers Point 2142 THEFT FROM MV < $5000 6 Fredericton 53
53 Barkers Point 2142 THEFT FROM MV < $5000 6 Fredericton 54
54 Barkers Point 2142 THEFT FROM MV < $5000 6 Fredericton 55
55 Barkers Point 2142 THEFT FROM MV < $5000 6 Fredericton 56
56 Barkers Point 2142 THEFT FROM MV < $5000 6 Fredericton 57
57 Barkers Point 2142 THEFT FROM MV < $5000 6 Fredericton 58
58 Barkers Point 2142 THEFT FROM MV < $5000 6 Fredericton 59
100 Sandyville 2142 THEFT FROM MV < $5000 5 Fredericton 101
107 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 108
108 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 109
109 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 110
110 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 111
111 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 112
112 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 113
113 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 114
114 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 115
115 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 116
116 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 117
117 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 118
118 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 119
119 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 120
120 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 121
121 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 122
122 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 123
123 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 124
124 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 125
125 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 126
126 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 127
127 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 128
128 South Devon 2142 THEFT FROM MV < $5000 4 Fredericton 129
151 Sandyville 2142 THEFT FROM MV < $5000 5 Fredericton 152
156 Knob Hill 2142 THEFT FROM MV < $5000 5 Fredericton 157
165 Youngs Crossing 2142 THEFT FROM MV < $5000 4 Fredericton 166
166 Youngs Crossing 2142 THEFT FROM MV < $5000 4 Fredericton 167
167 Youngs Crossing 2142 THEFT FROM MV < $5000 4 Fredericton 168
168 Youngs Crossing 2142 THEFT FROM MV < $5000 4 Fredericton 169
169 Youngs Crossing 2142 THEFT FROM MV < $5000 4 Fredericton 170
170 Youngs Crossing 2142 THEFT FROM MV < $5000 4 Fredericton 171
201 Marysville 2142 THEFT FROM MV < $5000 5 Fredericton 202
252 Marysville 2142 THEFT FROM MV < $5000 5 Fredericton 253
278 Douglas 2142 THEFT FROM MV < $5000 1 Fredericton 279
280 McLeod Hill 2142 THEFT FROM MV < $5000 2 Fredericton 281
281 McLeod Hill 2142 THEFT FROM MV < $5000 2 Fredericton 282
301 Marysville 2142 THEFT FROM MV < $5000 0 Fredericton 302
302 Marysville 2142 THEFT FROM MV < $5000 5 Fredericton 303
303 Marysville 2142 THEFT FROM MV < $5000 5 Fredericton 304
304 Marysville 2142 THEFT FROM MV < $5000 5 Fredericton 305
305 Marysville 2142 THEFT FROM MV < $5000 5 Fredericton 306
306 Marysville 2142 THEFT FROM MV < $5000 5 Fredericton 307
307 Marysville 2142 THEFT FROM MV < $5000 5 Fredericton 308
308 Marysville 2142 THEFT FROM MV < $5000 5 Fredericton 309
330 Saint Mary's First Nation 2142 THEFT FROM MV < $5000 3 Fredericton 331
349 Sandyville 2142 THEFT FROM MV < $5000 5 Fredericton 350
354 Nashwaaksis 2142 THEFT FROM MV < $5000 1 Fredericton 355
355 Nashwaaksis 2142 THEFT FROM MV < $5000 1 Fredericton 356
356 Nashwaaksis 2142 THEFT FROM MV < $5000 1 Fredericton 357
357 Nashwaaksis 2142 THEFT FROM MV < $5000 1 Fredericton 358
358 Nashwaaksis 2142 THEFT FROM MV < $5000 1 Fredericton 359
359 Nashwaaksis 2142 THEFT FROM MV < $5000 1 Fredericton 360
360 Nashwaaksis 2142 THEFT FROM MV < $5000 1 Fredericton 361
361 Nashwaaksis 2142 THEFT FROM MV < $5000 1 Fredericton 362
362 Nashwaaksis 2142 THEFT FROM MV < $5000 1 Fredericton 363
377 Northbrook Heights 2142 THEFT FROM MV < $5000 2 Fredericton 378
378 Northbrook Heights 2142 THEFT FROM MV < $5000 2 Fredericton 379
379 Northbrook Heights 2142 THEFT FROM MV < $5000 1 Fredericton 380
380 Northbrook Heights 2142 THEFT FROM MV < $5000 2 Fredericton 381
381 Northbrook Heights 2142 THEFT FROM MV < $5000 2 Fredericton 382
388 Heron Springs 2142 THEFT FROM MV < $5000 2 Fredericton 389
389 Heron Springs 2142 THEFT FROM MV < $5000 2 Fredericton 390
400 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 401
401 Downtown 2142 THEFT FROM MV < $5000 11 Fredericton 402
402 Downtown 2142 THEFT FROM MV < $5000 11 Fredericton 403
403 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 404
404 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 405
405 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 406
408 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 409
410 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 411
411 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 412
412 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 413
413 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 414
414 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 415
415 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 416
416 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 417
417 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 418
418 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 419
419 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 420
420 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 421
421 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 422
422 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 423
506 Downtown 2142 THEFT FROM MV < $5000 10 Fredericton 507
520 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 521
521 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 522
522 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 523
523 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 524
524 Fulton Heights 2142 THEFT FROM MV < $5000 2 Fredericton 525
525 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 526
526 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 527
527 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 528
528 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 529
529 Fulton Heights 2142 THEFT FROM MV < $5000 2 Fredericton 530
530 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 531
531 Fulton Heights 2142 THEFT FROM MV < $5000 3 Fredericton 532
569 Main Street 2142 THEFT FROM MV < $5000 2 Fredericton 570
570 Main Street 2142 THEFT FROM MV < $5000 3 Fredericton 571
571 Main Street 2142 THEFT FROM MV < $5000 2 Fredericton 572
572 Main Street 2142 THEFT FROM MV < $5000 2 Fredericton 573
573 Main Street 2142 THEFT FROM MV < $5000 3 Fredericton 574
574 Main Street 2142 THEFT FROM MV < $5000 2 Fredericton 575
575 Main Street 2142 THEFT FROM MV < $5000 2 Fredericton 576
576 Main Street 2142 THEFT FROM MV < $5000 2 Fredericton 577
577 Main Street 2142 THEFT FROM MV < $5000 2 Fredericton 578
578 Main Street 2142 THEFT FROM MV < $5000 2 Fredericton 579
604 Golf Club 2142 THEFT FROM MV < $5000 12 Fredericton 605
614 Gilridge Estates 2142 THEFT FROM MV < $5000 1 Fredericton 615
622 Nethervue Minihome Park 2142 THEFT FROM MV < $5000 12 Fredericton 623
625 Monteith / Talisman 2142 THEFT FROM MV < $5000 12 Fredericton 626
626 Monteith / Talisman 2142 THEFT FROM MV < $5000 12 Fredericton 627
631 Garden Creek 2142 THEFT FROM MV < $5000 12 Fredericton 632
640 Highpoint Ridge 2142 THEFT FROM MV < $5000 12 Fredericton 641
641 Highpoint Ridge 2142 THEFT FROM MV < $5000 12 Fredericton 642
642 Highpoint Ridge 2142 THEFT FROM MV < $5000 12 Fredericton 643
643 Highpoint Ridge 2142 THEFT FROM MV < $5000 12 Fredericton 644
650 Golf Club 2142 THEFT FROM MV < $5000 12 Fredericton 651
651 Golf Club 2142 THEFT FROM MV < $5000 12 Fredericton 652
653 Golf Club 2142 THEFT FROM MV < $5000 12 Fredericton 654
752 Golf Club 2142 THEFT FROM MV < $5000 12 Fredericton 753
764 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 765
765 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 766
766 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 767
767 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 768
768 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 769
769 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 770
770 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 771
771 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 772
772 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 773
773 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 774
774 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 775
775 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 776
776 Woodstock Road 2142 THEFT FROM MV < $5000 0 Fredericton 777
777 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 778
778 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 779
779 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 780
780 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 781
781 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 782
787 Sunshine Gardens 2142 THEFT FROM MV < $5000 10 Fredericton 788
788 Sunshine Gardens 2142 THEFT FROM MV < $5000 10 Fredericton 789
789 Sunshine Gardens 2142 THEFT FROM MV < $5000 10 Fredericton 790
790 Sunshine Gardens 2142 THEFT FROM MV < $5000 10 Fredericton 791
791 Sunshine Gardens 2142 THEFT FROM MV < $5000 10 Fredericton 792
792 Sunshine Gardens 2142 THEFT FROM MV < $5000 10 Fredericton 793
793 Sunshine Gardens 2142 THEFT FROM MV < $5000 10 Fredericton 794
809 Platt 2142 THEFT FROM MV < $5000 0 Fredericton 810
810 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 811
811 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 812
812 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 813
813 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 814
814 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 815
815 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 816
816 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 817
817 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 818
818 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 819
819 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 820
820 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 821
821 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 822
822 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 823
823 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 824
824 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 825
825 Platt 2142 THEFT FROM MV < $5000 0 Fredericton 826
826 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 827
827 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 828
828 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 829
829 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 830
830 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 831
831 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 832
832 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 833
833 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 834
835 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 836
836 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 837
837 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 838
838 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 839
839 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 840
840 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 841
841 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 842
842 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 843
843 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 844
844 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 845
845 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 846
846 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 847
847 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 848
848 Platt 2142 THEFT FROM MV < $5000 11 Fredericton 849
849 Platt 2142 THEFT FROM MV < $5000 10 Fredericton 850
855 Southwood Park 2142 THEFT FROM MV < $5000 7 Fredericton 856
856 Southwood Park 2142 THEFT FROM MV < $5000 7 Fredericton 857
857 Southwood Park 2142 THEFT FROM MV < $5000 7 Fredericton 858
865 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 866
866 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 867
867 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 868
868 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 869
869 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 870
871 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 872
875 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 876
880 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 881
881 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 882
886 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 887
887 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 888
892 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 893
893 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 894
898 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 899
899 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 900
900 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 901
901 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 902
902 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 903
903 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 904
904 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 905
905 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 906
906 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 907
907 Skyline Acrea 2142 THEFT FROM MV < $5000 8 Fredericton 908
913 Poet's Hill 2142 THEFT FROM MV < $5000 8 Fredericton 914
914 Poet's Hill 2142 THEFT FROM MV < $5000 8 Fredericton 915
922 Dun's Crossing 2142 THEFT FROM MV < $5000 8 Fredericton 923
923 Dun's Crossing 2142 THEFT FROM MV < $5000 8 Fredericton 924
924 Dun's Crossing 2142 THEFT FROM MV < $5000 8 Fredericton 925
925 Dun's Crossing 2142 THEFT FROM MV < $5000 8 Fredericton 926
926 Dun's Crossing 2142 THEFT FROM MV < $5000 8 Fredericton 927
927 Dun's Crossing 2142 THEFT FROM MV < $5000 8 Fredericton 928
928 Dun's Crossing 2142 THEFT FROM MV < $5000 8 Fredericton 929
929 Dun's Crossing 2142 THEFT FROM MV < $5000 8 Fredericton 930
930 Dun's Crossing 2142 THEFT FROM MV < $5000 8 Fredericton 931
938 Southwood Park 2142 THEFT FROM MV < $5000 7 Fredericton 939
939 Southwood Park 2142 THEFT FROM MV < $5000 7 Fredericton 940
940 Southwood Park 2142 THEFT FROM MV < $5000 7 Fredericton 941
941 Southwood Park 2142 THEFT FROM MV < $5000 7 Fredericton 942
946 The Hill 2142 THEFT FROM MV < $5000 9 Fredericton 947
947 The Hill 2142 THEFT FROM MV < $5000 9 Fredericton 948
948 The Hill 2142 THEFT FROM MV < $5000 9 Fredericton 949
949 The Hill 2142 THEFT FROM MV < $5000 10 Fredericton 950
950 The Hill 2142 THEFT FROM MV < $5000 10 Fredericton 951
951 The Hill 2142 THEFT FROM MV < $5000 11 Fredericton 952
952 The Hill 2142 THEFT FROM MV < $5000 9 Fredericton 953
954 The Hill 2142 THEFT FROM MV < $5000 10 Fredericton 955
955 The Hill 2142 THEFT FROM MV < $5000 10 Fredericton 956
956 The Hill 2142 THEFT FROM MV < $5000 9 Fredericton 957
957 The Hill 2142 THEFT FROM MV < $5000 9 Fredericton 958
969 Forest Hill 2142 THEFT FROM MV < $5000 8 Fredericton 970
970 Forest Hill 2142 THEFT FROM MV < $5000 8 Fredericton 971
971 Forest Hill 2142 THEFT FROM MV < $5000 8 Fredericton 972
972 Forest Hill 2142 THEFT FROM MV < $5000 8 Fredericton 973
973 Forest Hill 2142 THEFT FROM MV < $5000 8 Fredericton 974
974 Forest Hill 2142 THEFT FROM MV < $5000 8 Fredericton 975
975 Forest Hill 2142 THEFT FROM MV < $5000 8 Fredericton 976
976 Forest Hill 2142 THEFT FROM MV < $5000 8 Fredericton 977
989 Lincoln Heights 2142 THEFT FROM MV < $5000 7 Fredericton 990
996 Diamond Street 2142 THEFT FROM MV < $5000 1 Fredericton 997
1027 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1028
1028 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1029
1029 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1030
1030 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1031
1031 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1032
1032 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1033
1033 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1034
1034 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1035
1035 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1036
1036 College Hill 2142 THEFT FROM MV < $5000 11 Fredericton 1037
1060 Brookside Estates 2142 THEFT FROM MV < $5000 2 Fredericton 1061
1061 Brookside Estates 2142 THEFT FROM MV < $5000 2 Fredericton 1062
1062 Brookside Estates 2142 THEFT FROM MV < $5000 2 Fredericton 1063
1116 Lincoln 2142 THEFT FROM MV < $5000 7 Fredericton 1117
1124 Colonial heights 2142 THEFT FROM MV < $5000 12 Fredericton 1125
1125 Colonial heights 2142 THEFT FROM MV < $5000 12 Fredericton 1126
1126 Colonial heights 2142 THEFT FROM MV < $5000 12 Fredericton 1127
1127 Colonial heights 2142 THEFT FROM MV < $5000 12 Fredericton 1128
1128 Colonial heights 2142 THEFT FROM MV < $5000 11 Fredericton 1129
1129 Colonial heights 2142 THEFT FROM MV < $5000 11 Fredericton 1130
1131 Garden Place 2142 THEFT FROM MV < $5000 12 Fredericton 1132
1132 Garden Place 2142 THEFT FROM MV < $5000 12 Fredericton 1133
1133 Garden Place 2142 THEFT FROM MV < $5000 12 Fredericton 1134
1144 Waterloo Row 2142 THEFT FROM MV < $5000 11 Fredericton 1145
1145 Waterloo Row 2142 THEFT FROM MV < $5000 11 Fredericton 1146
1146 Waterloo Row 2142 THEFT FROM MV < $5000 11 Fredericton 1147
1151 University Of New Brunswick 2142 THEFT FROM MV < $5000 11 Fredericton 1152
1152 University Of New Brunswick 2142 THEFT FROM MV < $5000 11 Fredericton 1153
1153 University Of New Brunswick 2142 THEFT FROM MV < $5000 11 Fredericton 1154
1154 University Of New Brunswick 2142 THEFT FROM MV < $5000 11 Fredericton 1155
1163 Saint Thomas University 2142 THEFT FROM MV < $5000 11 Fredericton 1164
1173 Williams / Hawkins Area 2142 THEFT FROM MV < $5000 2 Fredericton 1174
1174 Williams / Hawkins Area 2142 THEFT FROM MV < $5000 2 Fredericton 1175
1175 Williams / Hawkins Area 2142 THEFT FROM MV < $5000 2 Fredericton 1176
1176 Williams / Hawkins Area 2142 THEFT FROM MV < $5000 2 Fredericton 1177
1177 Williams / Hawkins Area 2142 THEFT FROM MV < $5000 2 Fredericton 1178
1178 Williams / Hawkins Area 2142 THEFT FROM MV < $5000 2 Fredericton 1179
1181 McKnight 2142 THEFT FROM MV < $5000 2 Fredricton 1182
1187 Shadowood Estates 2142 THEFT FROM MV < $5000 2 Fredericton 1188
1188 Shadowood Estates 2142 THEFT FROM MV < $5000 2 Fredericton 1189
1240 Lian / Valcore 2142 THEFT FROM MV < $5000 12 Fredericton 1241
1284 North Devon 2142 THEFT FROM MV < $5000 4 Fredericton 1285
1285 North Devon 2142 THEFT FROM MV < $5000 4 Fredericton 1286
1286 North Devon 2142 THEFT FROM MV < $5000 4 Fredericton 1287
1287 North Devon 2142 THEFT FROM MV < $5000 4 Fredericton 1288
1288 North Devon 2142 THEFT FROM MV < $5000 4 Fredericton 1289
1289 North Devon 2142 THEFT FROM MV < $5000 4 Fredericton 1290
1290 North Devon 2142 THEFT FROM MV < $5000 4 Fredericton 1291
1302 Rail Side 2142 THEFT FROM MV < $5000 12 Fredericton 1303
1306 Rail Side 2142 THEFT FROM MV < $5000 12 Fredericton 1307
1316 Silverwood 2142 THEFT FROM MV < $5000 12 Fredericton 1317
1317 Silverwood 2142 THEFT FROM MV < $5000 12 Fredericton 1318
1339 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1340
1340 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1341
1341 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1342
1342 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1343
1343 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1344
1344 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1345
1345 Prospect 2142 THEFT FROM MV < $5000 11 Fredericton 1346
1346 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1347
1347 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1348
1348 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1349
1349 Prospect 2142 THEFT FROM MV < $5000 9 Fredericton 1350
1369 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1370
1370 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1371
1371 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1372
1372 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1373
1377 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1378
1380 Hanwell North 2142 THEFT FROM MV < $5000 12 Fredericton 1381
1381 Hanwell North 2142 THEFT FROM MV < $5000 12 Fredericton 1382
1382 Hanwell North 2142 THEFT FROM MV < $5000 12 Fredericton 1383
1387 Montogomery / Prospect East 2142 THEFT FROM MV < $5000 11 Fredericton 1388
1388 Montogomery / Prospect East 2142 THEFT FROM MV < $5000 11 Fredericton 1389
1389 Montogomery / Prospect East 2142 THEFT FROM MV < $5000 9 Fredericton 1390
1403 Fredericton South 2142 THEFT FROM MV < $5000 7 Fredericton 1404
1408 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 1409
1409 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 1410
1410 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 1411
1411 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 1412
1412 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 1413
1413 Fredericton South 2142 THEFT FROM MV < $5000 12 Fredericton 1414
1420 Woodstock Road 2142 THEFT FROM MV < $5000 12 Fredericton 1421
1421 Woodstock Road 2142 THEFT FROM MV < $5000 10 Fredericton 1422
1437 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1438
1438 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1439
1439 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1440
1440 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1441
1441 North Devon 2142 THEFT FROM MV < $5000 3 Fredericton 1442
1459 Monteith / Talisman 2142 THEFT FROM MV < $5000 12 Fredericton 1460
In [21]:
mvcrime_data = mvcrime_df.groupby(['Neighbourhood']).size().to_frame(name='Count').reset_index()
mvcrime_data
Out[21]:
Neighbourhood Count
0 Barkers Point 8
1 Brookside Estates 3
2 College Hill 10
3 Colonial heights 6
4 Diamond Street 1
5 Douglas 1
6 Downtown 21
7 Dun's Crossing 9
8 Forest Hill 8
9 Fredericton South 20
10 Fulton Heights 12
11 Garden Creek 1
12 Garden Place 3
13 Gilridge Estates 1
14 Golf Club 5
15 Hanwell North 3
16 Heron Springs 2
17 Highpoint Ridge 4
18 Knob Hill 1
19 Lian / Valcore 1
20 Lincoln 1
21 Lincoln Heights 11
22 Main Street 10
23 Marysville 10
24 McKnight 1
25 McLeod Hill 2
26 Monteith / Talisman 3
27 Montogomery / Prospect East 3
28 Nashwaaksis 9
29 Nethervue Minihome Park 1
30 North Devon 17
31 Northbrook Heights 5
32 Platt 40
33 Poet's Hill 2
34 Prospect 11
35 Rail Side 2
36 Saint Mary's First Nation 1
37 Saint Thomas University 1
38 Sandyville 3
39 Shadowood Estates 2
40 Silverwood 2
41 Skyline Acrea 13
42 South Devon 22
43 Southwood Park 7
44 Sunshine Gardens 7
45 The Hill 11
46 University Of New Brunswick 4
47 Waterloo Row 3
48 Williams / Hawkins Area 6
49 Woodstock Road 20
50 Youngs Crossing 6
In [22]:
mvcrime_data.describe()
Out[22]:
Count
count 51.000000
mean 6.980392
std 7.457855
min 1.000000
25% 2.000000
50% 4.000000
75% 10.000000
max 40.000000
In [23]:
mvcrime_data.rename({'Platt': 'Plat'},inplace=True)
mvcrime_data.rename(index=str, columns={'Neighbourhood':'Neighbourh','Count':'MVCrime_Count'}, inplace=True)
mvcrime_data
Out[23]:
Neighbourh MVCrime_Count
0 Barkers Point 8
1 Brookside Estates 3
2 College Hill 10
3 Colonial heights 6
4 Diamond Street 1
5 Douglas 1
6 Downtown 21
7 Dun's Crossing 9
8 Forest Hill 8
9 Fredericton South 20
10 Fulton Heights 12
11 Garden Creek 1
12 Garden Place 3
13 Gilridge Estates 1
14 Golf Club 5
15 Hanwell North 3
16 Heron Springs 2
17 Highpoint Ridge 4
18 Knob Hill 1
19 Lian / Valcore 1
20 Lincoln 1
21 Lincoln Heights 11
22 Main Street 10
23 Marysville 10
24 McKnight 1
25 McLeod Hill 2
26 Monteith / Talisman 3
27 Montogomery / Prospect East 3
28 Nashwaaksis 9
29 Nethervue Minihome Park 1
30 North Devon 17
31 Northbrook Heights 5
32 Platt 40
33 Poet's Hill 2
34 Prospect 11
35 Rail Side 2
36 Saint Mary's First Nation 1
37 Saint Thomas University 1
38 Sandyville 3
39 Shadowood Estates 2
40 Silverwood 2
41 Skyline Acrea 13
42 South Devon 22
43 Southwood Park 7
44 Sunshine Gardens 7
45 The Hill 11
46 University Of New Brunswick 4
47 Waterloo Row 3
48 Williams / Hawkins Area 6
49 Woodstock Road 20
50 Youngs Crossing 6
In [24]:
world_geo = r'world_countries.json' # geojson file

fredericton_c_map = folium.Map(location=[45.91, -66.65], width=1000, height=750,zoom_start=12)

fredericton_c_map
Out[24]:
In [ ]:
 
In [25]:
## Motor Vehicle Crime <$5000 Count 
fredericton_geo = r.json()
threshold_scale = np.linspace(mvcrime_data['MVCrime_Count'].min(), mvcrime_data['MVCrime_Count'].max(),6,dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale[-1] = threshold_scale[-1]+1

fredericton_c_map.choropleth(geo_data=fredericton_geo,data=mvcrime_data,columns=['Neighbourh', 'MVCrime_Count'],key_on='feature.properties.Neighbourh',
    threshold_scale=threshold_scale, fill_color='YlOrRd',fill_opacity=0.7,line_opacity=0.1,legend_name='Fredericton Neighbourhoods')
fredericton_c_map
Out[25]:
In [ ]:
 

Is it possible the higher rate of crime in the downtown area is due to population density?

In [26]:
demog_df = pd.read_csv("Census_Tract_Demographics.csv")
demog_df.head()
Out[26]:
FID OBJECTID DBUID DAUID CDUID CTUID CTNAME DBuid_1 DBpop2011 DBtdwell20 DBurdwell2 Shape_Leng Shape_Area CTIDLINK Shape__Area Shape__Length
0 1 501 1310024304 13100243 1310 3200002.0 2.0 1310024304 60 25 22 0.007462 0.000003 3200002 0.000003 0.007462
1 2 502 1310032004 13100320 1310 3200010.0 10.0 1310032004 15 3 3 0.009008 0.000003 3200010 0.000003 0.009008
2 3 503 1310017103 13100171 1310 3200014.0 14.0 1310017103 0 0 0 0.010602 0.000007 3200014 0.000007 0.010602
3 4 504 1310018301 13100183 1310 3200012.0 12.0 1310018301 108 60 50 0.039599 0.000068 3200012 0.000068 0.039599
4 5 505 1310022905 13100229 1310 3200007.0 7.0 1310022905 129 47 44 0.011833 0.000005 3200007 0.000005 0.011834
In [27]:
# Population Density 
world_geo = r'world_countries.json' # geojson file
fredericton_d_map = folium.Map(location=[45.94, -66.63], width=1200, height=750,zoom_start=12)
fredericton_d_map

threshold_scale = np.linspace(demog_df['DBpop2011'].min(),demog_df['DBpop2011'].max(),6,dtype=int)
threshold_scale = threshold_scale.tolist()
threshold_scale[-1] = threshold_scale[-1]+1

fredericton_d_map.choropleth(geo_data=demog_geo,data=demog_df,columns=['OBJECTID','DBpop2011'],key_on='feature.properties.OBJECTID',
    threshold_scale=threshold_scale,fill_color='PuBuGn',fill_opacity=0.7, line_opacity=0.1,legend_name='Fredericton Population Density')
fredericton_d_map
Out[27]:

Let's look at specific locations in Fredericton

In [28]:
pointbook = 'Fredericton Locations.xlsx'

workbook_2 = pd.ExcelFile(pointbook)
print(workbook_2.sheet_names)
['Sheet1']
In [29]:
location_df = workbook_2.parse('Sheet1')
location_df
Out[29]:
Location Latitude Longitude
0 Knowledge Park 45.931143 -66.652700
1 Fredericton Downtown 45.963026 -66.383550
2 Fredericton Hill 45.948512 -66.656045
3 Nashwaaksis 45.983382 -66.644856
4 University of New Brunswick 45.948121 -66.641406
5 Devon 45.968802 -66.622738
6 New Maryland 45.892795 -66.683673
7 Marysville 45.978913 -66.589491
8 Skyline Acres 45.931827 -66.640339
9 Hanwell 45.902315 -66.755113

Add location markers to map

In [30]:
for lat, lng, point in zip(location_df['Latitude'], location_df['Longitude'], location_df['Location']):
    label = '{}'.format(point)
    label = folium.Popup(label, parse_html=True)
    folium.CircleMarker([lat, lng],radium=1,popup=label,color='blue',fill=True,fill_color='#3186cc',fill_opacity=0.7,
        parse_html=False).add_to(fredericton_c_map)
fredericton_c_map
Out[30]:
In [ ]:
 

Explore Fredericton Neighbourhoods

Define Foursquare Credentials and Version

In [31]:
CLIENT_ID = 'ZDZQDDOB32ORH4QOA5FW12XS1JAQ3ABKLJ3S4H5JZPMJ33QU'
CLIENT_SECRET = 'WQZEFI2A5QB443AI2L1I4BS32RDCGJ0SY2MMDD4BEDHL0VTU'
VERSION = '20181201'

print('Your credentails:')
print('CLIENT_ID: ' + CLIENT_ID)
print('CLIENT_SECRET:' + CLIENT_SECRET)
Your credentails:
CLIENT_ID: ZDZQDDOB32ORH4QOA5FW12XS1JAQ3ABKLJ3S4H5JZPMJ33QU
CLIENT_SECRET:WQZEFI2A5QB443AI2L1I4BS32RDCGJ0SY2MMDD4BEDHL0VTU

Let's take a look at nearby venues

In [32]:
def getNearbyVenues(names, latitudes, longitudes, radius=1000, LIMIT=100):
    
    venues_list=[]
    for name, lat, lng in zip(names, latitudes, longitudes):
        print(name)
            
        # create the API request URL
        url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
            CLIENT_ID, 
            CLIENT_SECRET, 
            VERSION, 
            lat, 
            lng, 
            radius, 
            LIMIT)
            
        # make the GET request
        results = requests.get(url).json()["response"]['groups'][0]['items']
        
        # return only relevant information for each nearby venue
        venues_list.append([(
            name, 
            lat, 
            lng,            
            v['venue']['name'], 
            v['venue']['id'],
            v['venue']['location']['lat'], 
            v['venue']['location']['lng'],  
            v['venue']['categories'][0]['name']) for v in results])

    nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
    nearby_venues.columns = ['Location', 
                  'Location Latitude', 
                  'Location Longitude', 
                  'Venue',
                  'Venue id',                
                  'Venue Latitude', 
                  'Venue Longitude', 
                  'Venue Category'        
                   ]
    
    return(nearby_venues)
In [33]:
fredericton_data_venues = getNearbyVenues(names=location_df['Location'],
                                   latitudes=location_df['Latitude'],
                                   longitudes=location_df['Longitude']
                                  )
Knowledge Park
Fredericton Downtown
Fredericton Hill
Nashwaaksis
University of New Brunswick
Devon
New Maryland
Marysville
Skyline Acres
Hanwell
In [34]:
print(fredericton_data_venues.shape)
fredericton_data_venues
(98, 8)
Out[34]:
Location Location Latitude Location Longitude Venue Venue id Venue Latitude Venue Longitude Venue Category
0 Knowledge Park 45.931143 -66.652700 Costco Wholesale 4e18ab92183880768f43bff6 45.927034 -66.663447 Warehouse Store
1 Knowledge Park 45.931143 -66.652700 PetSmart 4bbca501a0a0c9b6078f1a0f 45.929768 -66.659939 Pet Store
2 Knowledge Park 45.931143 -66.652700 Montana's 4e50406e62844166699b0780 45.931511 -66.662507 Restaurant
3 Knowledge Park 45.931143 -66.652700 Boston Pizza 4b64944af964a52041bf2ae3 45.938123 -66.660037 Sports Bar
4 Knowledge Park 45.931143 -66.652700 Michaels 4c489858417b20a13b82e1a9 45.929965 -66.659548 Arts & Crafts Store
5 Knowledge Park 45.931143 -66.652700 Alcool NB Liquor 4b77335df964a5202c872ee3 45.930680 -66.664180 Liquor Store
6 Knowledge Park 45.931143 -66.652700 Best Buy 5520124a498e0467bb6e81c8 45.937673 -66.660380 Electronics Store
7 Knowledge Park 45.931143 -66.652700 Booster Juice 4c42414e520fa59334f9caac 45.935198 -66.663602 Smoothie Shop
8 Knowledge Park 45.931143 -66.652700 Wal-Mart 4bad313ff964a5208c373be3 45.934081 -66.663539 Big Box Store
9 Knowledge Park 45.931143 -66.652700 H&M 509c3265498efdffc5739a0f 45.935196 -66.663290 Clothing Store
10 Knowledge Park 45.931143 -66.652700 Dairy Queen 4b86f05bf964a52009a731e3 45.938004 -66.659442 Fast Food Restaurant
11 Knowledge Park 45.931143 -66.652700 Dairy Queen (Treat) 4cc6123cbde8f04d9ce0b44b 45.934520 -66.663988 Ice Cream Shop
12 Knowledge Park 45.931143 -66.652700 Winners 4caa46a744a8224b96e42640 45.930427 -66.659758 Clothing Store
13 Knowledge Park 45.931143 -66.652700 East Side Mario's 4b55d89bf964a520a2f227e3 45.931376 -66.663417 Italian Restaurant
14 Knowledge Park 45.931143 -66.652700 McDonald's 4c6e9ef665eda09377e951d0 45.934575 -66.663319 Fast Food Restaurant
15 Knowledge Park 45.931143 -66.652700 Home Sense 54024f60498ee424eedb7bf9 45.930528 -66.660103 Department Store
16 Knowledge Park 45.931143 -66.652700 The Shoe company 4bd76dfa5cf276b0fb469b00 45.929636 -66.660449 Shoe Store
17 Knowledge Park 45.931143 -66.652700 Avalon Spa Uptown 4cd99e0f51fc8cfa4369f05d 45.930774 -66.660927 Spa
18 Knowledge Park 45.931143 -66.652700 Wicker Emporium 4e6baff588772457c4fd1968 45.930897 -66.661338 Furniture / Home Store
19 Knowledge Park 45.931143 -66.652700 Dollarama 4ba3dd18f964a520d86738e3 45.930897 -66.661714 Discount Store
20 Knowledge Park 45.931143 -66.652700 Bed Bath & Beyond 5083f283e4b0bf87c15e9ea1 45.930097 -66.662166 Furniture / Home Store
21 Knowledge Park 45.931143 -66.652700 GAP Factory Store 50a8f005e4b0e4f42e033a2a 45.930211 -66.662416 Clothing Store
22 Knowledge Park 45.931143 -66.652700 carter's | OshKosh B'gosh 50a51363e4b0a3e2f7db76bf 45.929978 -66.662966 Kids Store
23 Knowledge Park 45.931143 -66.652700 Hallmark 4cd96cf651fc8cfa522eef5d 45.930646 -66.663745 Gift Shop
24 Knowledge Park 45.931143 -66.652700 NB Liquor 5985f08b6cf01a7e38b85fba 45.930228 -66.664395 Liquor Store
25 Knowledge Park 45.931143 -66.652700 Corbett Center 57854d05498e301b3b5a4448 45.929733 -66.664601 Shopping Plaza
26 Knowledge Park 45.931143 -66.652700 Costco Food Court 53693053498ef3e4ea63560f 45.927383 -66.663544 Fast Food Restaurant
27 Knowledge Park 45.931143 -66.652700 Sleep Country 555b5660498eae864c440e77 45.929074 -66.664605 Mattress Store
28 Knowledge Park 45.931143 -66.652700 RW&CO. 4eb9622a6c2590eb89b5de15 45.935160 -66.663871 Women's Store
29 Knowledge Park 45.931143 -66.652700 Rôtisserie St-Hubert 57164569498e9bb9e88d52b0 45.929838 -66.664749 Restaurant
30 Fredericton Hill 45.948512 -66.656045 YMCA Fredericton 4e93476b8231bf0d17ba3e24 45.953217 -66.649478 Gym
31 Fredericton Hill 45.948512 -66.656045 20 Twenty Club 4c5388b0f5f3d13ac74ba5f8 45.951042 -66.648112 Bar
32 Fredericton Hill 45.948512 -66.656045 Shoppers Drug Mart 4fb699dc7bebbeb2a6c7ba88 45.942627 -66.655523 Pharmacy
33 Fredericton Hill 45.948512 -66.656045 Subway 4bae3571f964a52076923be3 45.940931 -66.657445 Sandwich Place
34 Fredericton Hill 45.948512 -66.656045 Canadian Tire 4bb52ba72ea19521201caa2f 45.944409 -66.666820 Hardware Store
35 Fredericton Hill 45.948512 -66.656045 Tim Hortons 4dc29f89d4c07da169fbf84b 45.943720 -66.646907 Coffee Shop
36 Fredericton Hill 45.948512 -66.656045 The Aitken University Centre - UNB 4b6458eff964a52052ac2ae3 45.941644 -66.663667 Hockey Arena
37 Fredericton Hill 45.948512 -66.656045 Queen Square Park 4b7acb0ef964a520113d2fe3 45.950961 -66.648245 Park
38 Fredericton Hill 45.948512 -66.656045 Papa John's Pizza 4ecc29f59adfd1f5b5c7bbb1 45.956655 -66.657285 Pizza Place
39 Fredericton Hill 45.948512 -66.656045 Greco 4cfc0660c51fa1cdd3d7e92b 45.954055 -66.647290 Pizza Place
40 Fredericton Hill 45.948512 -66.656045 Dollarama 4b71d1d0f964a520525e2de3 45.956785 -66.657225 Discount Store
41 Fredericton Hill 45.948512 -66.656045 Dick's Grocery Store 4c545e5db426ef3b11cc7e8a 45.941957 -66.663877 Smoke Shop
42 Fredericton Hill 45.948512 -66.656045 Tingley's Ice Cream 4c13c001b7b9c9284e12aa37 45.957087 -66.655855 Ice Cream Shop
43 Fredericton Hill 45.948512 -66.656045 Domino's Pizza 50f9bbc75d24acebc259244d 45.957177 -66.656638 Pizza Place
44 Fredericton Hill 45.948512 -66.656045 Goody Shop 4b8580edf964a5201d6231e3 45.951172 -66.644000 Bakery
45 Nashwaaksis 45.983382 -66.644856 Peters Meat, Seafood & Lobster Market 4c4e04ecfb742d7fe7bba62d 45.976652 -66.649765 Grocery Store
46 Nashwaaksis 45.983382 -66.644856 Tim Hortons 4b742f31f964a520b7cb2de3 45.975294 -66.646977 Coffee Shop
47 Nashwaaksis 45.983382 -66.644856 The Northside Market 50270b2ae4b042eaf816ee61 45.977779 -66.635003 Farmers Market
48 Nashwaaksis 45.983382 -66.644856 Shoppers Drug Mart 4c745e08db52b1f781f775dc 45.976515 -66.648534 Pharmacy
49 Nashwaaksis 45.983382 -66.644856 Subway 4bc5db23693695213a9a8488 45.976886 -66.648661 Sandwich Place
50 Nashwaaksis 45.983382 -66.644856 Subway 4c87f3b4bf40a1cd09fd08b4 45.989114 -66.652061 Sandwich Place
51 Nashwaaksis 45.983382 -66.644856 Kentucky Fried Chicken 4eefb90ba69ddc7bcb336081 45.975903 -66.646846 Fast Food Restaurant
52 Nashwaaksis 45.983382 -66.644856 Nashwaaksis Field House 4b73436cf964a52016a52de3 45.984849 -66.643635 Gym
53 Nashwaaksis 45.983382 -66.644856 KFC 4c9267139199bfb7786c14df 45.975907 -66.646870 Fast Food Restaurant
54 Nashwaaksis 45.983382 -66.644856 Tim Hortons 4c0104cf360a9c74bb11d9a0 45.989221 -66.652208 Coffee Shop
55 Nashwaaksis 45.983382 -66.644856 Mike's Old Fashioned Bakery 4d67fde7709bb60c5eacb014 45.976560 -66.650030 Bakery
56 Nashwaaksis 45.983382 -66.644856 Cox Electronics 4d07eab6611ff04d4f4718fb 45.976112 -66.649222 Electronics Store
57 Nashwaaksis 45.983382 -66.644856 A Pile Of Scrap! 4e9f0e9b93ad5d11f3d36ba1 45.984398 -66.633329 Arts & Crafts Store
58 Nashwaaksis 45.983382 -66.644856 Jim Gilberts Wheels And Deals 4b9a7ef5f964a520b6ba35e3 45.980784 -66.633311 Auto Dealership
59 Nashwaaksis 45.983382 -66.644856 The North Side Market 501c19f7e4b01c57ff1b1212 45.977837 -66.635168 Farmers Market
60 Nashwaaksis 45.983382 -66.644856 Avalon SalonSpa 4bc31784920eb71312ec1c2c 45.974591 -66.644756 Spa
61 Nashwaaksis 45.983382 -66.644856 Tony Pepperoni 4c88f56dbbec6dcbe9f2d758 45.991888 -66.648599 Pizza Place
62 University of New Brunswick 45.948121 -66.641406 The Richard J. CURRIE Center - UNB 4dbae5806e815ab0de5d2637 45.946698 -66.637891 Basketball Court
63 University of New Brunswick 45.948121 -66.641406 Charlotte Street Arts Centre 4b7f0318f964a5203d1030e3 45.955620 -66.639324 Art Gallery
64 University of New Brunswick 45.948121 -66.641406 Sobeys 4b6727daf964a520493e2be3 45.954891 -66.645920 Grocery Store
65 University of New Brunswick 45.948121 -66.641406 YMCA Fredericton 4e93476b8231bf0d17ba3e24 45.953217 -66.649478 Gym
66 University of New Brunswick 45.948121 -66.641406 20 Twenty Club 4c5388b0f5f3d13ac74ba5f8 45.951042 -66.648112 Bar
67 University of New Brunswick 45.948121 -66.641406 The Cellar Pub & Grill - UNB 4b7ac93ef964a520b53c2fe3 45.945434 -66.641626 Pub
68 University of New Brunswick 45.948121 -66.641406 Harvey's 4bbdff85f57ba59320bdaeb9 45.953544 -66.645021 Burger Joint
69 University of New Brunswick 45.948121 -66.641406 Tim Hortons 4c865c1774d7b60c3f41a3d8 45.945185 -66.641545 Coffee Shop
70 University of New Brunswick 45.948121 -66.641406 Tim Hortons 4dc29f89d4c07da169fbf84b 45.943720 -66.646907 Coffee Shop
71 Devon 45.968802 -66.622738 New England Pizza 4c09984e7e3fc928b64bf282 45.967675 -66.629905 Pizza Place
72 Devon 45.968802 -66.622738 Wolastoq Wharf 4fbaafb0e4b0c7f68a419500 45.969927 -66.632486 Seafood Restaurant
73 Devon 45.968802 -66.622738 Pharmacie Jean Coutu 4eb9523077c8972738ac89b2 45.967766 -66.630551 Pharmacy
74 Devon 45.968802 -66.622738 Dairy Queen 4c5cab2894fd0f473c69c945 45.969077 -66.632059 Fast Food Restaurant
75 Devon 45.968802 -66.622738 Tim Hortons 4b5b0812f964a520d8df28e3 45.969381 -66.632730 Coffee Shop
76 Devon 45.968802 -66.622738 Henry Park 4c8e283dad01199c7923726d 45.963992 -66.620283 Baseball Field
77 Devon 45.968802 -66.622738 Giant Tiger 4c95354f58d4b60c80443029 45.967715 -66.630410 Department Store
78 Devon 45.968802 -66.622738 york arena 4b6c4f10f964a520792f2ce3 45.964888 -66.617110 Skating Rink
79 Devon 45.968802 -66.622738 St. Mary's Supermarket 4b9fa6adf964a520c93137e3 45.971945 -66.631248 Grocery Store
80 Devon 45.968802 -66.622738 Dixie Lee 4c5cacc5d25320a103fdc37a 45.962257 -66.624952 Fast Food Restaurant
81 Devon 45.968802 -66.622738 St Marys Smoke Shop 4ebddf8a4690d233887bf4a6 45.972270 -66.631348 Smoke Shop
82 Devon 45.968802 -66.622738 Carleton Park 4bce2eeb29d4b7138521a8dc 45.961182 -66.626310 Park
83 New Maryland 45.892795 -66.683673 State Farm Insurance, Shelly Pye 5674142f498e2d1993a51c08 45.894667 -66.682877 Home Service
84 New Maryland 45.892795 -66.683673 Village Lounge 4e18b776e4cd49a7e3ecc301 45.888272 -66.685532 Bar
85 New Maryland 45.892795 -66.683673 Baseball, Basketball, Tennis and Hockey In One... 4e48415862e148603b8b3fc2 45.890726 -66.692814 Baseball Field
86 New Maryland 45.892795 -66.683673 New Maryland Minor Baseball Batting Cage 4fba6e0ce4b0d55659f3f08c 45.890743 -66.692857 Baseball Field
87 New Maryland 45.892795 -66.683673 Oakland Farm & Lodge 531b663611d2036682e17b3f 45.895539 -66.672790 Farm
88 New Maryland 45.892795 -66.683673 Circle K 4b9e633ef964a5202fdf36e3 45.885412 -66.688995 Gas Station
89 Marysville 45.978913 -66.589491 Tim Hortons 4baa1b40f964a520174b3ae3 45.978193 -66.593041 Coffee Shop
90 Marysville 45.978913 -66.589491 Royals Field 4c573f916201e21edff8736e 45.980267 -66.588412 Baseball Stadium
91 Marysville 45.978913 -66.589491 Northside Pharmacy 4c8bee978018a1cdd1f2e7d2 45.980194 -66.588628 Pharmacy
92 Marysville 45.978913 -66.589491 Marysville Place 4ce6d19be1eeb60c512d99ae 45.980243 -66.588277 Park
93 Marysville 45.978913 -66.589491 Circle K 4bb88fe853649c74431847fb 45.979250 -66.593232 Gas Station
94 Skyline Acres 45.931827 -66.640339 Grant Harvey Centre 4f915a7ee4b01406ebc873ae 45.925002 -66.641004 Hockey Arena
95 Skyline Acres 45.931827 -66.640339 Kimble Field 4fdaa8c2e4b08f3358b1b3d1 45.930535 -66.631233 Baseball Field
96 Skyline Acres 45.931827 -66.640339 Mandarin Palace 4b786998f964a5204ecc2ee3 45.935440 -66.631007 Chinese Restaurant
97 Skyline Acres 45.931827 -66.640339 Oriental Pearl 4ec68431775bf65c02417199 45.930085 -66.629518 Chinese Restaurant
In [35]:
print('There are {} unique venue categories.'.format(len(fredericton_data_venues['Venue Category'].unique())))
There are 49 unique venue categories.
In [36]:
print('There are {} unique venues.'.format(len(fredericton_data_venues['Venue id'].unique())))
There are 95 unique venues.
In [37]:
univen = fredericton_data_venues.groupby('Location').nunique('Venue Category')
univen
Out[37]:
Location Location Latitude Location Longitude Venue Venue id Venue Latitude Venue Longitude Venue Category
Location
Devon 1 1 1 12 12 12 12 11
Fredericton Hill 1 1 1 15 15 15 15 13
Knowledge Park 1 1 1 30 30 30 30 23
Marysville 1 1 1 5 5 5 5 5
Nashwaaksis 1 1 1 15 17 17 17 13
New Maryland 1 1 1 6 6 6 6 5
Skyline Acres 1 1 1 4 4 4 4 3
University of New Brunswick 1 1 1 8 9 9 9 8
In [38]:
fredericton_data_venues.groupby('Venue Category').nunique()
Out[38]:
Location Location Latitude Location Longitude Venue Venue id Venue Latitude Venue Longitude Venue Category
Venue Category
Art Gallery 1 1 1 1 1 1 1 1
Arts & Crafts Store 2 2 2 2 2 2 2 1
Auto Dealership 1 1 1 1 1 1 1 1
Bakery 2 2 2 2 2 2 2 1
Bar 3 3 3 2 2 2 2 1
Baseball Field 3 3 3 4 4 4 4 1
Baseball Stadium 1 1 1 1 1 1 1 1
Basketball Court 1 1 1 1 1 1 1 1
Big Box Store 1 1 1 1 1 1 1 1
Burger Joint 1 1 1 1 1 1 1 1
Chinese Restaurant 1 1 1 2 2 2 2 1
Clothing Store 1 1 1 3 3 3 3 1
Coffee Shop 5 5 5 1 6 6 6 1
Department Store 2 2 2 2 2 2 2 1
Discount Store 2 2 2 1 2 2 2 1
Electronics Store 2 2 2 2 2 2 2 1
Farm 1 1 1 1 1 1 1 1
Farmers Market 1 1 1 2 2 2 2 1
Fast Food Restaurant 3 3 3 6 7 7 7 1
Furniture / Home Store 1 1 1 2 2 2 2 1
Gas Station 2 2 2 1 2 2 2 1
Gift Shop 1 1 1 1 1 1 1 1
Grocery Store 3 3 3 3 3 3 3 1
Gym 3 3 3 2 2 2 2 1
Hardware Store 1 1 1 1 1 1 1 1
Hockey Arena 2 2 2 2 2 2 2 1
Home Service 1 1 1 1 1 1 1 1
Ice Cream Shop 2 2 2 2 2 2 2 1
Italian Restaurant 1 1 1 1 1 1 1 1
Kids Store 1 1 1 1 1 1 1 1
Liquor Store 1 1 1 2 2 2 2 1
Mattress Store 1 1 1 1 1 1 1 1
Park 3 3 3 3 3 3 3 1
Pet Store 1 1 1 1 1 1 1 1
Pharmacy 4 4 4 3 4 4 4 1
Pizza Place 3 3 3 5 5 5 5 1
Pub 1 1 1 1 1 1 1 1
Restaurant 1 1 1 2 2 2 2 1
Sandwich Place 2 2 2 1 3 3 3 1
Seafood Restaurant 1 1 1 1 1 1 1 1
Shoe Store 1 1 1 1 1 1 1 1
Shopping Plaza 1 1 1 1 1 1 1 1
Skating Rink 1 1 1 1 1 1 1 1
Smoke Shop 2 2 2 2 2 2 2 1
Smoothie Shop 1 1 1 1 1 1 1 1
Spa 2 2 2 2 2 2 2 1
Sports Bar 1 1 1 1 1 1 1 1
Warehouse Store 1 1 1 1 1 1 1 1
Women's Store 1 1 1 1 1 1 1 1
In [ ]:
 

Analyze each Location

In [39]:
# one hot encoding
freddy_onehot = pd.get_dummies(fredericton_data_venues[['Venue Category']], prefix="", prefix_sep="")

# add neighbourhood column back to dataframe
freddy_onehot['Location'] = fredericton_data_venues['Location'] 

# move neighbourhood column to the first column
fixed_columns = [freddy_onehot.columns[-1]] + list(freddy_onehot.columns[:-1])
freddy_onehot = freddy_onehot[fixed_columns]

freddy_onehot.head()
Out[39]:
Location Art Gallery Arts & Crafts Store Auto Dealership Bakery Bar Baseball Field Baseball Stadium Basketball Court Big Box Store Burger Joint Chinese Restaurant Clothing Store Coffee Shop Department Store Discount Store Electronics Store Farm Farmers Market Fast Food Restaurant Furniture / Home Store Gas Station Gift Shop Grocery Store Gym Hardware Store Hockey Arena Home Service Ice Cream Shop Italian Restaurant Kids Store Liquor Store Mattress Store Park Pet Store Pharmacy Pizza Place Pub Restaurant Sandwich Place Seafood Restaurant Shoe Store Shopping Plaza Skating Rink Smoke Shop Smoothie Shop Spa Sports Bar Warehouse Store Women's Store
0 Knowledge Park 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
1 Knowledge Park 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 Knowledge Park 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
3 Knowledge Park 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
4 Knowledge Park 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
In [40]:
freddy_onehot.shape
Out[40]:
(98, 50)

Group rows by location and by the mean of the frequency of occurrence of each category

In [41]:
freddy_grouped = freddy_onehot.groupby('Location').mean().reset_index()
freddy_grouped
Out[41]:
Location Art Gallery Arts & Crafts Store Auto Dealership Bakery Bar Baseball Field Baseball Stadium Basketball Court Big Box Store Burger Joint Chinese Restaurant Clothing Store Coffee Shop Department Store Discount Store Electronics Store Farm Farmers Market Fast Food Restaurant Furniture / Home Store Gas Station Gift Shop Grocery Store Gym Hardware Store Hockey Arena Home Service Ice Cream Shop Italian Restaurant Kids Store Liquor Store Mattress Store Park Pet Store Pharmacy Pizza Place Pub Restaurant Sandwich Place Seafood Restaurant Shoe Store Shopping Plaza Skating Rink Smoke Shop Smoothie Shop Spa Sports Bar Warehouse Store Women's Store
0 Devon 0.000000 0.000000 0.000000 0.000000 0.000000 0.083333 0.0 0.000000 0.000000 0.000000 0.0 0.0 0.083333 0.083333 0.000000 0.000000 0.000000 0.000000 0.166667 0.000000 0.000000 0.000000 0.083333 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.083333 0.000000 0.083333 0.083333 0.000000 0.000000 0.000000 0.083333 0.000000 0.000000 0.083333 0.083333 0.000000 0.000000 0.000000 0.000000 0.000000
1 Fredericton Hill 0.000000 0.000000 0.000000 0.066667 0.066667 0.000000 0.0 0.000000 0.000000 0.000000 0.0 0.0 0.066667 0.000000 0.066667 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.066667 0.066667 0.066667 0.000000 0.066667 0.000000 0.000000 0.000000 0.000000 0.066667 0.000000 0.066667 0.200000 0.000000 0.000000 0.066667 0.000000 0.000000 0.000000 0.000000 0.066667 0.000000 0.000000 0.000000 0.000000 0.000000
2 Knowledge Park 0.000000 0.033333 0.000000 0.000000 0.000000 0.000000 0.0 0.000000 0.033333 0.000000 0.0 0.1 0.000000 0.033333 0.033333 0.033333 0.000000 0.000000 0.100000 0.066667 0.000000 0.033333 0.000000 0.000000 0.000000 0.000000 0.000000 0.033333 0.033333 0.033333 0.066667 0.033333 0.000000 0.033333 0.000000 0.000000 0.000000 0.066667 0.000000 0.000000 0.033333 0.033333 0.000000 0.000000 0.033333 0.033333 0.033333 0.033333 0.033333
3 Marysville 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.2 0.000000 0.000000 0.000000 0.0 0.0 0.200000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.200000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.200000 0.000000 0.200000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4 Nashwaaksis 0.000000 0.058824 0.058824 0.058824 0.000000 0.000000 0.0 0.000000 0.000000 0.000000 0.0 0.0 0.117647 0.000000 0.000000 0.058824 0.000000 0.117647 0.117647 0.000000 0.000000 0.000000 0.058824 0.058824 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.058824 0.058824 0.000000 0.000000 0.117647 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.058824 0.000000 0.000000 0.000000
5 New Maryland 0.000000 0.000000 0.000000 0.000000 0.166667 0.333333 0.0 0.000000 0.000000 0.000000 0.0 0.0 0.000000 0.000000 0.000000 0.000000 0.166667 0.000000 0.000000 0.000000 0.166667 0.000000 0.000000 0.000000 0.000000 0.000000 0.166667 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6 Skyline Acres 0.000000 0.000000 0.000000 0.000000 0.000000 0.250000 0.0 0.000000 0.000000 0.000000 0.5 0.0 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.250000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7 University of New Brunswick 0.111111 0.000000 0.000000 0.000000 0.111111 0.000000 0.0 0.111111 0.000000 0.111111 0.0 0.0 0.222222 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.111111 0.111111 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.111111 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
In [42]:
freddy_grouped.shape
Out[42]:
(8, 50)
In [43]:
num_top_venues = 5

for hood in freddy_grouped['Location']:
    print("----"+hood+"----")
    temp = freddy_grouped[freddy_grouped['Location'] == hood].T.reset_index()
    temp.columns = ['venue','freq']
    temp = temp.iloc[1:]
    temp['freq'] = temp['freq'].astype(float)
    temp = temp.round({'freq': 2})
    print(temp.sort_values('freq', ascending=False).reset_index(drop=True).head(num_top_venues))
    print('\n')
----Devon----
                  venue  freq
0  Fast Food Restaurant  0.17
1      Department Store  0.08
2        Baseball Field  0.08
3         Grocery Store  0.08
4            Smoke Shop  0.08


----Fredericton Hill----
            venue  freq
0     Pizza Place  0.20
1  Hardware Store  0.07
2     Coffee Shop  0.07
3    Hockey Arena  0.07
4             Gym  0.07


----Knowledge Park----
                    venue  freq
0    Fast Food Restaurant  0.10
1          Clothing Store  0.10
2            Liquor Store  0.07
3  Furniture / Home Store  0.07
4              Restaurant  0.07


----Marysville----
              venue  freq
0  Baseball Stadium   0.2
1       Gas Station   0.2
2              Park   0.2
3          Pharmacy   0.2
4       Coffee Shop   0.2


----Nashwaaksis----
                  venue  freq
0        Sandwich Place  0.12
1        Farmers Market  0.12
2           Coffee Shop  0.12
3  Fast Food Restaurant  0.12
4                   Spa  0.06


----New Maryland----
            venue  freq
0  Baseball Field  0.33
1    Home Service  0.17
2             Bar  0.17
3     Gas Station  0.17
4            Farm  0.17


----Skyline Acres----
                venue  freq
0  Chinese Restaurant  0.50
1        Hockey Arena  0.25
2      Baseball Field  0.25
3          Restaurant  0.00
4      Ice Cream Shop  0.00


----University of New Brunswick----
           venue  freq
0    Coffee Shop  0.22
1    Art Gallery  0.11
2            Bar  0.11
3            Gym  0.11
4  Grocery Store  0.11


Now into a pandas dataframe

In [44]:
def return_most_common_venues(row, num_top_venues):
    row_categories = row.iloc[1:]
    row_categories_sorted = row_categories.sort_values(ascending=False)
    
    return row_categories_sorted.index.values[0:num_top_venues]
In [45]:
num_top_venues = 10

indicators = ['st', 'nd', 'rd']

# create columns according to number of top venues
columns = ['Location']
for ind in np.arange(num_top_venues):
    try:
        columns.append('{}{} Most Common Venue'.format(ind+1, indicators[ind]))
    except:
        columns.append('{}th Most Common Venue'.format(ind+1))

# create a new dataframe
location_venues_sorted = pd.DataFrame(columns=columns)
location_venues_sorted['Location'] = freddy_grouped['Location']

for ind in np.arange(freddy_grouped.shape[0]):
    location_venues_sorted.iloc[ind, 1:] = return_most_common_venues(freddy_grouped.iloc[ind, :], num_top_venues)

location_venues_sorted
Out[45]:
Location 1st Most Common Venue 2nd Most Common Venue 3rd Most Common Venue 4th Most Common Venue 5th Most Common Venue 6th Most Common Venue 7th Most Common Venue 8th Most Common Venue 9th Most Common Venue 10th Most Common Venue
0 Devon Fast Food Restaurant Pharmacy Seafood Restaurant Coffee Shop Pizza Place Department Store Baseball Field Skating Rink Smoke Shop Grocery Store
1 Fredericton Hill Pizza Place Hardware Store Sandwich Place Gym Hockey Arena Ice Cream Shop Discount Store Coffee Shop Park Pharmacy
2 Knowledge Park Fast Food Restaurant Clothing Store Furniture / Home Store Restaurant Liquor Store Women's Store Pet Store Department Store Gift Shop Warehouse Store
3 Marysville Coffee Shop Gas Station Baseball Stadium Park Pharmacy Women's Store Furniture / Home Store Fast Food Restaurant Farmers Market Farm
4 Nashwaaksis Sandwich Place Fast Food Restaurant Farmers Market Coffee Shop Pizza Place Gym Electronics Store Pharmacy Grocery Store Arts & Crafts Store
5 New Maryland Baseball Field Home Service Gas Station Bar Farm Women's Store Department Store Gift Shop Furniture / Home Store Fast Food Restaurant
6 Skyline Acres Chinese Restaurant Hockey Arena Baseball Field Women's Store Department Store Gift Shop Gas Station Furniture / Home Store Fast Food Restaurant Farmers Market
7 University of New Brunswick Coffee Shop Art Gallery Basketball Court Gym Burger Joint Pub Grocery Store Bar Discount Store Gift Shop

Cluster Fredericton Locations

Run k-means to cluster Locations into 5 clusters

In [46]:
# set number of clusters
kclusters = 5

freddy_grouped_clustering = freddy_grouped.drop('Location', 1)

# run k-means clustering
kmeans = KMeans(n_clusters=kclusters, random_state=0).fit(freddy_grouped_clustering)

# check cluster labels generated for each row in the dataframe
kmeans.labels_=[1, 1, 1, 0, 1, 4, 1, 3, 2, 1]
kmeans.labels_
Out[46]:
[1, 1, 1, 0, 1, 4, 1, 3, 2, 1]

Now creating a new dataframe including the cluster as well as the top 10 venues for each Location

In [47]:
freddy_merged = location_df

# add clustering labels
freddy_merged['Cluster Labels'] = kmeans.labels_

# merge fredericton_grouped with location df to add latitude/longitude for each location
freddy_merged = freddy_merged.join(location_venues_sorted.set_index('Location'), on='Location')

freddy_merged
Out[47]:
Location Latitude Longitude Cluster Labels 1st Most Common Venue 2nd Most Common Venue 3rd Most Common Venue 4th Most Common Venue 5th Most Common Venue 6th Most Common Venue 7th Most Common Venue 8th Most Common Venue 9th Most Common Venue 10th Most Common Venue
0 Knowledge Park 45.931143 -66.652700 1 Fast Food Restaurant Clothing Store Furniture / Home Store Restaurant Liquor Store Women's Store Pet Store Department Store Gift Shop Warehouse Store
1 Fredericton Downtown 45.963026 -66.383550 1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2 Fredericton Hill 45.948512 -66.656045 1 Pizza Place Hardware Store Sandwich Place Gym Hockey Arena Ice Cream Shop Discount Store Coffee Shop Park Pharmacy
3 Nashwaaksis 45.983382 -66.644856 0 Sandwich Place Fast Food Restaurant Farmers Market Coffee Shop Pizza Place Gym Electronics Store Pharmacy Grocery Store Arts & Crafts Store
4 University of New Brunswick 45.948121 -66.641406 1 Coffee Shop Art Gallery Basketball Court Gym Burger Joint Pub Grocery Store Bar Discount Store Gift Shop
5 Devon 45.968802 -66.622738 4 Fast Food Restaurant Pharmacy Seafood Restaurant Coffee Shop Pizza Place Department Store Baseball Field Skating Rink Smoke Shop Grocery Store
6 New Maryland 45.892795 -66.683673 1 Baseball Field Home Service Gas Station Bar Farm Women's Store Department Store Gift Shop Furniture / Home Store Fast Food Restaurant
7 Marysville 45.978913 -66.589491 3 Coffee Shop Gas Station Baseball Stadium Park Pharmacy Women's Store Furniture / Home Store Fast Food Restaurant Farmers Market Farm
8 Skyline Acres 45.931827 -66.640339 2 Chinese Restaurant Hockey Arena Baseball Field Women's Store Department Store Gift Shop Gas Station Furniture / Home Store Fast Food Restaurant Farmers Market
9 Hanwell 45.902315 -66.755113 1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
In [48]:
# create map
map_clusters = folium.Map(location=[latitude, longitude], zoom_start=11)

# set color scheme for the clusters
x = np.arange(kclusters)
ys = [i+x+(i*x)**2 for i in range(kclusters)]
colors_array = cm.rainbow(np.linspace(0, 1, len(ys)))
rainbow = [colors.rgb2hex(i) for i in colors_array]

# add markers to the map
markers_colors = []
for lat, lon, poi, cluster in zip(freddy_merged['Latitude'], freddy_merged['Longitude'], freddy_merged['Location'], freddy_merged['Cluster Labels']):
    label = folium.Popup(str(poi) + ' Cluster ' + str(cluster), parse_html=True)
    folium.CircleMarker([lat, lon], radius=5,popup=label,color=rainbow[cluster-1],fill=True,fill_color=rainbow[cluster-1],
        fill_opacity=0.7).add_to(map_clusters)
map_clusters
Out[48]:
In [ ]: